import {HomeModel} from "../../models/home/HomeModel.js";
import {config} from "../../config";

const homeModel = new HomeModel()

Page({

    /**
     * 页面的初始数据
     */
    data: {

        carouselList: [],
        carouseCaList: [],
        newsList: [],
        schoolList: [],
        teacherList: [],
        subjectList: [],
        studentList: [],

        city: {
            fullname: "东莞市",
            lat: 23.02067,
            lng: 113.75179,
            name: "东莞",
        },

        type: 1,
        current: 1,
        pageSize: 10


    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.init(this.data.type, this.data.current, this.data.pageSize)

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },


    init(type, current, pageSize) {

        const getPic = homeModel.getCarousePic()
        const getCourseCa = homeModel.getCourseCategory()
        const getNews = homeModel.getNews(type, current, pageSize)
        const getSchool = homeModel.getSchool(type, current, pageSize)
        const getTeacher = homeModel.getTeacher(type, current, pageSize)
        const getSubject = homeModel.getSubject(type, current, pageSize)
        const getStudent = homeModel.getStudent(type, current, pageSize)

        Promise.all([getPic, getCourseCa, getNews, getSchool, getTeacher, getSubject, getStudent])
            .then(res => {

                this.setData({
                    carouselList: res[0],
                    carouseCaList: res[1],
                    newsList: res[2],
                    schoolList: res[3],
                    teacherList: res[4],
                    subjectList: res[5],
                    studentList: res[6]
                })
            })
            .catch(err => {
                console.log(err);
            })
    },


    /**
     * 点击搜索框
     */
    onClickSearch() {
        wx.navigateTo({
            url: '../search/index'
        })
    },

    /**
     * 点击城市名称
     */
    onClickCity() {

        wx.navigateTo({
            url: '../city/index'
        })

    },

    /**
     * 点击 分类标签
     * @param event
     */
    clickCa(event) {

        let data = event.detail
        data.type = config.caToSubject
        let dataJson = JSON.stringify(data)
        wx.navigateTo({
          url: '../subject/all/index?data='+dataJson
        })
    },

    /**
     * 点击新闻 更多
     * @param event
     */
    newsMoreTap(event) {
        console.log(event);
    },

    /**
     * 点击新闻item
     * @param event
     */
    newsItemTap(event) {
        let newsId = event.target.dataset.ns_id

    },


    /**
     * 点击学校 更多
     * @param event
     */
    schoolMoreTap(event) {

        wx.navigateTo({
            url: '../school/all/index'
        })
    },

    /**
     * 点击 学校
     * @param event
     */
    schoolTap(event) {
        let sId = event.target.dataset.sid
        wx.navigateTo({
          url: '../school/detail/index?sId='+sId
        })
    },

    /**
     * 点击老师 更多
     * @param event
     */
    teacherMoreTap(event){
        wx.navigateTo({
          url: '../teacher/all/index'
        })
    },

    /**
     * 点击老师
     * @param event
     */
    teacherClick(event) {
        let tId = event.target.dataset.tid
        wx.navigateTo({
          url: '../teacher/detail/index?tId=' + tId
        })
    },

    /**
     * 点击课程 更多
     * @param event
     */
    subjectMoreTap(event){
        wx.navigateTo({
          url: '../subject/all/index'

        })
    },

    /**
     * 点击课程 更多
     */
    studentMoreTap(){
        wx.navigateTo({
          url: '../student/all/index'
        })
    },

    /**
     * 点击课程
     * @param event
     */
    subjectTap(event){

        let cId = event.target.dataset.cid
        wx.navigateTo({
          url: '../subject/detail/index?cId='+cId
        })
    },
    
    /**
     * 点击 学生
     * @param event
     */
    studentTap(event){
        let stId = event.target.dataset.stid
        wx.navigateTo({
          url: '../student/detail/index?stId='+ stId
        })
    }


})